package com.ly.honor.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ly.honor.entity.User;
import com.ly.honor.exception.HonorException;
import com.ly.honor.service.UserService;
import com.ly.honor.util.RedisUtil;
import com.ly.honor.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author LiYang
 * @Date 2022/11/17 18:34
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/user")
public class LoginController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserService userService;


    @PostMapping("login")
    public Result login(@RequestBody JSONObject jsonObject){
//        var captcha = redisUtil.get("captcha");
//        if(captcha != null){
//            if(!captcha.toString().equals(jsonObject.get("code"))){
//                return Result.ok().code(20001).message("验证码错误");
//            }
//        }
        //{code: 20000, data: {token: "admin-token"}}
        var user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, jsonObject.get("username")));
        if(ObjectUtils.isEmpty(user)){
            throw new HonorException("找不到该用户");
        }
        if(!user.getPassword().equals(jsonObject.get("password"))){
            throw new HonorException("密码错误");
        }
        Map<String,Object> map = new HashMap<>();
        String token = IdUtil.simpleUUID();
        System.out.println(jsonObject);
        if("admin".equals(user.getUsername())){
            token = "admin-token";
            map.put("token",token);
        }else {
            map.put("token", token);
        }
        redisUtil.set(token,user,3000);
        return Result.ok(map);
    }

    //info
    @GetMapping("info")
    public Result info(String token){
        var objectMapper = new ObjectMapper();
        System.out.println("token = " + token);
        //{"code":20000,"data":
        // {"roles":["admin"],
        // "introduction":"I am a super administrator",
        // "avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
        // "name":"Super Admin"}}
        Map<String,Object> map = new HashMap<>();
        if("admin-token".equals(token)){
            map.put("roles","admin");
        }else {
            map.put("roles","common");
        }
//        map.put("roles","admin");
        Object userInfo = redisUtil.get(token);
        var user = objectMapper.convertValue(userInfo, User.class);
        map.put("introduction","I am a super administrator");
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name",user);
        return Result.ok(map);
    }
}
